package com.tenton.servlet;

import com.tenton.pojo.User;
import com.tenton.service.Impl.UserServiceImpl;
import com.tenton.service.UserService;
import com.tenton.utils.ConstantClassUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/18
 * @Author: Tenton
 * @Description:
 */
@WebServlet(name = "RegistServlet",urlPatterns = "/registServlet")
public class RegistServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        UserService userService = new UserServiceImpl();
        //获取前端注册页面表单数据
        //用户名
        String userName = request.getParameter("userName");
        //密码
        String password = request.getParameter("password");
        //姓名
        String fullName = request.getParameter("fullName");
        //电话
        String tel = request.getParameter("tel");
        //住址
        String address = request.getParameter("address");
        //所在国家
        String country = request.getParameter("country");
        //所在城市
        String city = request.getParameter("city");
        //邮箱
        String email = request.getParameter("email");
        //兴趣爱好
        String[] hobbies = request.getParameterValues("hobby");
        String hobby = "";
        for (String str : hobbies) {
            if (hobby == ""){
                hobby = hobby + str;
            }else {
                hobby = hobby + ",";
                hobby = hobby + str;
            }
        }
        //密保问题
        String problem = "";
        String str = request.getParameter("problem");
        if (ConstantClassUtils.PROBLEM1.equals(str)){
            problem = "故乡地址";
        }else if (ConstantClassUtils.PROBLEM2.equals(str)){
            problem = "家庭住址";
        } else if (ConstantClassUtils.PROBLEM3.equals(str)){
            problem = "公司名称";
        } else{
            problem = "爱人姓氏";
        }
        //密保答案
        String answer = request.getParameter("answers");
        User user = new User(userName, password, fullName, tel, address, country, city, email, hobby, problem, answer);
        int i = userService.insertUser(user);
        //添加成功
        if (i > 0) {
            //跳转到登录界面
            //弹窗提示
            response.getWriter().print("<script>alert('注册成功!')</script>");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        } else {
            response.getWriter().write("注册失败，2秒后重写跳转回注册界面");
            response.setHeader("Refresh", "3;URL=regist.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
